<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2018/7/19
 * Time: 11:20
 */
namespace app\index\controller;

use app\common\model\WorkSearch;

class Search extends Index
{
    public function index()
    {
        $keyword=input('keyword','','strval');
        $uid=$this->auth->id;
        if (empty($uid)){
            if ($check=WorkSearch::get(['keyword'=>$keyword])){
                model('WorkSearch')->where(['id'=>$check['id']])->inc('hot')->update();
            }else{
                WorkSearch::create(['keyword'=>$keyword,'hot'=>1]);
            }
        }
        $keyword_part=part_words($keyword);
        $keyword_part=array_column($keyword_part,'word');
//        $pids=db('work_words')->where('words','in',$keyword)->column('pid');
        $data=db('work_words')->alias('a')->join('work b','a.pid=b.id','left')
                ->where('a.words','in',$keyword_part)
                ->field('b.*')
                ->paginate(5);
        $data_page=$data->jsonSerialize();
        $this->assign('keyword',$keyword);
        $this->assign('data_page',$data_page);
        $this->assign('data',$data);

        $cid=input('cid');
        $cate=db('category')->where('id',$cid)->value('name');
        $child=db('category')->where('pid',$cid)->order('weigh asc')->select();
        $this->assign('child',$child);

        $link_word=db('work_words')->where('words','in',$keyword_part)->distinct(true)->column('words');
        $link_word=array_unique(array_merge($keyword_part,$link_word));
        $this->assign('link_word',$link_word);
        return view();
    }
}